Javascript:
function sample(c_id) { document.getElementById("result").value=""; var x = document.getElementById("result").value=c_id; }
HTML:
<div id="1" onclick="sample(this.id)">one</div>
<div id="2" onclick="sample(this.id)">Two</div>
<input type="text" value="" id="result">
Please Help Me...
请帮我...
4 个解决方案
#1
1
You'll need to keep an array of values that have already been added to the input. When you toggle each number, you can have it removed from the array with splice()
or added to the array with push()
. Then all you need to do is join the values with a comma, and apply it to the value of your input
:
您需要保留已添加到输入中的值数组。当您切换每个数字时,可以使用splice()将其从数组中删除,或者使用push()将其添加到数组中。然后,您需要做的就是用逗号连接值,并将其应用于输入的值:
var taken = [];
function sample(c_id) {
var inp = document.getElementById("result");
var ind = taken.indexOf(c_id);
if(ind > -1){
taken.splice(ind,1);
taken = taken;
} else {
taken.push(c_id);
}
inp.value = taken.join(',');
}
Note: It is invalid to start your id
s with a number. They should start with a letter.
注意:使用数字启动ID无效。他们应该以一封信开头。
#2
0
not sure what you are trying to do there.
不确定你在那里做什么。
You don't need to pass the id to the function, that can be got with .id
on a html element. ie. document.getElementById('someElement').id;
你不需要将id传递给函数,可以在html元素上使用.id。即。 。的document.getElementById( 'someElement')ID;
Try to get your javascript in an external document too, as is you are going to place your code inline then you may as well hard code the id anyway. (not suggesting that obviously)
尝试在外部文档中获取您的javascript,因为您要将代码内联,然后您也可以硬编码id。 (显然没有暗示)
#3
0
If I'm not wrong I think you want something like this:
如果我没错,我想你想要这样的东西:
$("div").click(function(){
$("#result").val($("#result").val() + ", " + $(this).attr("id"))
})
or if you want something similar to your code just do this:
或者如果你想要与你的代码类似的东西,就这样做:
$("div").click(sample($(this).attr("id")));
function sample(c_id)
{
document.getElementById("result").val(document.getElementById("result").val() + ", " + c_id);
var x = document.getElementById("result").val();
}
#4
0
function sample(c_id) {
var result = document.getElementById('result'),
resultVal = result.value;
result.value = resultVal == '' || resultVal == undefined ? c_id : resultVal', '+c_id;
}
#1
1
You'll need to keep an array of values that have already been added to the input. When you toggle each number, you can have it removed from the array with splice()
or added to the array with push()
. Then all you need to do is join the values with a comma, and apply it to the value of your input
:
您需要保留已添加到输入中的值数组。当您切换每个数字时,可以使用splice()将其从数组中删除,或者使用push()将其添加到数组中。然后,您需要做的就是用逗号连接值,并将其应用于输入的值:
var taken = [];
function sample(c_id) {
var inp = document.getElementById("result");
var ind = taken.indexOf(c_id);
if(ind > -1){
taken.splice(ind,1);
taken = taken;
} else {
taken.push(c_id);
}
inp.value = taken.join(',');
}
Note: It is invalid to start your id
s with a number. They should start with a letter.
注意:使用数字启动ID无效。他们应该以一封信开头。
#2
0
not sure what you are trying to do there.
不确定你在那里做什么。
You don't need to pass the id to the function, that can be got with .id
on a html element. ie. document.getElementById('someElement').id;
你不需要将id传递给函数,可以在html元素上使用.id。即。 。的document.getElementById( 'someElement')ID;
Try to get your javascript in an external document too, as is you are going to place your code inline then you may as well hard code the id anyway. (not suggesting that obviously)
尝试在外部文档中获取您的javascript,因为您要将代码内联,然后您也可以硬编码id。 (显然没有暗示)
#3
0
If I'm not wrong I think you want something like this:
如果我没错,我想你想要这样的东西:
$("div").click(function(){
$("#result").val($("#result").val() + ", " + $(this).attr("id"))
})
or if you want something similar to your code just do this:
或者如果你想要与你的代码类似的东西,就这样做:
$("div").click(sample($(this).attr("id")));
function sample(c_id)
{
document.getElementById("result").val(document.getElementById("result").val() + ", " + c_id);
var x = document.getElementById("result").val();
}
#4
0
function sample(c_id) {
var result = document.getElementById('result'),
resultVal = result.value;
result.value = resultVal == '' || resultVal == undefined ? c_id : resultVal', '+c_id;
}