同名输入表格更改值beform submit

时间:2016-04-01 07:12:12

标签: jquery html forms

我有以下表格:

<form action="xxx">
  <label>
    <input type="radio" name="dataType" value="newsite" checked> newsite
  </label>
  <label>
    <input type="radio" name="dataType" value="newsiteAndOtherInfo"> newsiteAndOtherInfo
  </label>
  <div id="newsite">  
    <input name="site" value="1"/>
  </div>
  <div id="newsiteAndOtherInfo" style="display:none">
    <input name="site" value="2"/>
    <input name="OtherInfo" />
    ......
  </input>
  <input type="submit"/>
</form>

<script>
$('input[type="radio"]').click(function() {
    if ($(this).attr("value") == "newsite") {
        $("#newsiteAndOtherInfo").hide();
        $("#newsite").show();
    }
    if ($(this).attr("value") == "newsiteAndOtherInfo") {
        $("#newsite").hide();
        $("#newsiteAndOtherInfo").show();
    }
});
</script>

提交时,表单数据为:

  

网站:1,2

虽然我只希望将可见输入值传递给服务器。

我知道$.ajax可以轻松完成。如

var site = $("[name=site]:visible").val();
var data = {"site":site};
$.ajax({
  url:"xxx",
  type: 'post',
  data: data,
  success: function(response) {
  }
});

但如果我同步使用表单提交,则更改值不起作用。

$("form").submit(function() {
      formdata(this);
      return true;
    });
});

function formdata(form) {
    var site = $(form).find("[name=site]:hidden").val();
    form.site.value = site;  // not work
}

我想在提交之前必须有方法更改数据但却无法找到它。

[编辑]

由于我的问题不明确,请在此处添加一些补充。

实际上,两个div是两种表单数据,一些输入是相同的字段。

0 个答案:

没有答案
相关问题