原始问题基于完全错误的逻辑:O
$(function() {
var x=0,y=1;
$("#div_id").find("input:text").each(function (i, input) {
$(this).val(""+(i % 2 == 0)?x++:y++);
});
});
Run Code Online (Sandbox Code Playgroud)
如果我从值中删除""+,我会得到错误的结果

而不是我想要的,这是:

如果我使用道具也一样:
$(this).prop("value",""+(i % 2 == 0)?x++:y++);
Run Code Online (Sandbox Code Playgroud)
如果我在++前面加上相同的话
我在俯瞰什么?这是显而易见的事吗?
更新:我完全错过了这里的船.
这是我打算编写的代码,它的工作原理没有""
$(function() {
var x=0,y=1;
var inputs = $("#div_id").find("input:text");
var y = Math.ceil(inputs.size()/2);
inputs.each(function (i, input) {
$(this).prop("value",(i % 2 == 0)?++x:++y);
});
});
Run Code Online (Sandbox Code Playgroud)
"" + (i % 2 == 0) ? x++ : y++
Run Code Online (Sandbox Code Playgroud)
被解释为(参见优先规则)
("" + (i % 2 == 0)) ? x++ : y++
Run Code Online (Sandbox Code Playgroud)
并且("" + (i % 2 == 0))总是truthy,因为两者"true"和"false"非空字符串.所以在你的例子中,你一直在使用x++,忽略了y++分支.