jQuery 将文本输入值发布到 ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17632895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post text input value to ajax
提问by dilettante
I have a functional AJAX post that's working great. My client requested an additional text input field, and for the life of me, I can't get it. The script is long so I'll give you the relevant bits in hopes that it's a matter of syntax.
我有一个功能强大的 AJAX 帖子,效果很好。我的客户请求了一个额外的文本输入字段,但在我的一生中,我无法得到它。脚本很长,所以我会给你相关的部分,希望它是一个语法问题。
HTML for my input:
我的输入的 HTML:
<input type="text" name="why" id="why" maxlength="70">
Javascript call
Javascript 调用
$(document).on("click", ".take-claim-link", function(){
var id= $(this).attr('data-id');
var point= $(this).attr('data-point');
var val= $(this).attr('data-val');
var type = $(this).attr('data-type');
var why = $("#why").val();
var follow = document.getElementById("follow-" + id + "-"+ type);
var followuser='0';
if (follow.checked) {
var followuser = "1";
}
var takeurl = '/api/responseClaim.json';
var newPrice ='';
var newData = $.ajax({
type: "POST",
url: takeurl,
dataType: "json",
data: { point: point, claimId: id, type: val,why: why,follow: followuser }
})
(etc).
It returns nothing for "why".
对于“为什么”,它不返回任何内容。
The funny thing: if I change the variable to var why = 'sample why';
it passes. Likewise, if I change my input to <input type="text" name="why" id="why" maxlength="70" value="some value">
it reads it. So I think the problem is in the way I'm defining it.
有趣的是:如果我将变量更改为var why = 'sample why';
通过。同样,如果我改变我的输入,<input type="text" name="why" id="why" maxlength="70" value="some value">
它会读取它。所以我认为问题在于我定义它的方式。
But what? I've also tried var why = $("input#why").val();
and document.getElementById("why").value;
但是什么?我也试着var why = $("input#why").val();
和document.getElementById("why").value;
回答by Hieu Nguyen
There could be another element with same id="why"
in your HTML code. So try to search for it. Or change the id of above input to something else.
id="why"
您的 HTML 代码中可能有另一个相同的元素。所以尝试搜索它。或者将上述输入的 id 更改为其他内容。
Optionally you can try to get that input value with:
(可选)您可以尝试使用以下方法获取该输入值:
var why = $('input[name="why"]').val();
Hope it helps.
希望能帮助到你。