dongwh1992 2017-05-17 04:19
浏览 44
已采纳

尝试使用JavaScript解析将值应用于隐藏的输入变量

I'm using a JavaScript function to get the amount to post. The only way I can get the amount is to parse the item_name input variable from the form. It works on Chrome, not on IE. By the way, it hooks to PayPal using PHP and MySQL.

function ReadForm(obj1) {
  var holeObjectValue = obj1.item_name.value;
  var splitFields = holeObjectValue.split("$");
  obj1.amount.value = splitFields[1];
}
<form action="" method="post" name="form1" target="_self">
  <input name="item_name" type="radio" onclick="ReadForm(this.form, false);" value="Please Select from above $ 100" checked="checked" />Please Select from above $ 100<br />
  <input name="item_name" type="radio" onclick="ReadForm(this.form, false);" value="Please Select from above $ 200" checked="checked" />Please Select from above $ 200<br />
  <input name="amount" type="hidden" value="" />
  <input type="submit" value="submit" />
</form>

</div>
  • 写回答

3条回答 默认 最新

  • duanduji2986 2017-05-17 04:51
    关注

    There are a few things going wrong. In:

      var holeObjectValue = obj1.item_name.value; 
    

    since there are two radio buttons with the same name, that returns a NodeList which doesn't have a vlaue property. To get the value of the selected radio, you can loop over the radios or use a selector:

    var selected = obj1.querySelector('input[name=item_name]:checked');
    

    In your markup you have made both radio button checked. It is the nature of radio buttons that only one can be checked at a time, so remove the checked attribute from one of them.

    Lastly, you should not need to parse the value to get what you want, so make the value "200" or whatever, e.g.

    <input name="item_name"  type="radio" onclick="ReadForm(this.form, false);"
      value="200">Please Select from above $ 200<br>
    

    However, many browsers in use don't support such complex selectors so a looping solution is more compatible (and not a lot to write), so:

    function ReadForm(obj1) {
      var radios = obj1.item_name;
      var holeObjectValue = null;
    
      for (var i = 0, iLen = radios.length; i < iLen; i++) {
        if (radios[i].checked) {
          holeObjectValue = radios[i].value;
          break;
        }
      }
    
      if (!holeObjectValue) return;
      obj1.amount.value = holeObjectValue;
    }
    <form action="" method="post" name="form1" target="_self" 
      onsubmit="return false"> <!-- prevent submit for testing -->
      <input name="item_name" type="radio" onclick="ReadForm(this.form, false);"
       value="100" checked>Please Select from above $ 100<br>
      <input name="item_name" type="radio" onclick="ReadForm(this.form, false);"
       value="200" >Please Select from above $ 200<br>
      <input name="amount" value=""> <!-- show for testing -->
      <input type="submit">
    </form>

    The last issue you have is that one radio is selected by default, but the value isn't written to the amount field if the user doesn't click on the radio. I'll leave that for you to solve.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效