Bug Tracker

Opened 7 years ago

Closed 7 years ago

#13029 closed bug (notabug)

form attributes function ".val()" cannot set value with RegExp Backreference

Reported by: sugita takayuki <sugilog@…> Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.3
Keywords: Cc:
Blocked by: Blocking:

Description

sample code:

<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
  <p>case1</p>
  <select id="period" name="period">
    <option value="201301">2013/01</option>
    <option value="201302">2013/02</option>
    <option value="201303" selected>2013/03</option>
    <option value="201304">2013/04</option>
  </select>

  <input type="text" id="period_field" name="period_field" value="201212" />


  <p>case2</p>
  <select id="period_with_var" name="period_with_var">
    <option value="201301">2013/01</option>
    <option value="201302">2013/02</option>
    <option value="201303" selected>2013/03</option>
    <option value="201304">2013/04</option>
  </select>

  <input type="text" id="period_field_with_var" name="period_field_with_var" value="201212" />


  <p>case3</p>
  <select id="period_with_raw" name="period_with_raw">
    <option value="201301">2013/01</option>
    <option value="201302">2013/02</option>
    <option value="201303" selected>2013/03</option>
    <option value="201304">2013/04</option>
  </select>

  <input type="text" id="period_field_with_raw" name="period_field_with_raw" value="201212" />

  <script type="text/javascript">
    jQuery(function(){
      // case 1; use RegExp backreference.
      if ( /period=(\d{6})/.test(location.hash) ) {
        console.log(RegExp.$1);
      }
      if ( /period=(\d{6})/.test(location.hash) ) {
        jQuery("#period").val(RegExp.$1);
      }
      if ( /period=(\d{6})/.test(location.hash) ) {
        jQuery("#period_field").val(RegExp.$1);
      }

      // case 2; use RegExp backreference with variable.
      if ( /period=(\d{6})/.test(location.hash) ) {
        var period = RegExp.$1;
        console.log(period);
        jQuery("#period_with_var").val(period);
        jQuery("#period_field_with_var").val(period);
      }
    });

    // case 3; use raw javascript function.
    var setVal = function(selector, _value) {
      document.body.querySelector(selector).value = _value;
    }
    if ( /period=(\d{6})/.test(location.hash) ) {
      setVal("#period_with_raw", RegExp.$1);
    }
    if ( /period=(\d{6})/.test(location.hash) ) {
      setVal("#period_field_with_raw", RegExp.$1);
    }
  </script>
</body>
</html>

in this case, only case1 won't work.

  1. save sample code into html file.
  2. access on browser with fragment ( example: #period=201302 )
  3. fields of case2 and case3 has value "201302", but select of case1 has value "201301"; text field of case1 has empty value.

Change History (1)

comment:1 Changed 7 years ago by dmethvin

Resolution: notabug
Status: newclosed

Please ask for help on the forum.

Note: See TracTickets for help on using tickets.