简体   繁体   English

如何在javascript中打印输入文本类型id的值

[英]how can i print value of input text type id in javascript

<input type=text name=input id="in1" value=1 onclick=inputs()>
<input type=text name=input id="in2" value=2 onclick=inputs()>
<input type=text name=input id="in3" value=3 onclick=inputs()>
<input type=text name=input id="in4" value=4 onclick=inputs()>
<script type=text/javascript>
function inputs()
{
var val=document.getElementById();
document.write(val);
}
</script>

Actually, i want only onclick's input id in variable val. 实际上,我只想在变量val中使用onclick的输入id。 please help me. 请帮我。

Maybe everyone else is reading a different question than I am but it seems like you want the value of the ID, not the value of the input. 也许其他人都在阅读与我不同的问题,但似乎你想要ID的值,而不是输入的值。

<input type=text name=input id="in1" value=1 onclick=inputs(this.id)>
<input type=text name=input id="in2" value=2 onclick=inputs(this.id)>
<input type=text name=input id="in3" value=3 onclick=inputs(this.id)>
<input type=text name=input id="in4" value=4 onclick=inputs(this.id)>
<script type=text/javascript>
function inputs(val)
{
    document.write(val);
}
</script>

This will give you the ID of that element, not its value. 这将为您提供该元素的ID,而不是其值。 Which is what I hope I'm reading from this oddly worded question. 这是我希望我从这个措辞奇怪的问题中读到的。

<input type=text name=input id="in1" value=1 onclick=inputs(this.id)>
<input type=text name=input id="in2" value=2 onclick=inputs(this.id)>
<input type=text name=input id="in3" value=3 onclick=inputs(this.id)>
<input type=text name=input id="in4" value=4 onclick=inputs(this.id)>
<script type=text/javascript>
function inputs(id)
{
var val=id;
document.write(val);
}
</script>

Try this 尝试这个

<input type="text" name="input" id="in1" value="1" onclick="inputs(this.id)">
<input type=text name=input id="in2" value=2 onclick="inputs(this.id)">
<input type=text name=input id="in3" value=3 onclick="inputs(this.id)">
<input type=text name=input id="in4" value=4 onclick="inputs(this.id)">
<script type=text/javascript>
function inputs(id)
{
var val=id
alert(val);
}
</script>

DEMO DEMO

<input type=text name=input id="in1" value=1 onclick=inputs(this)>
<input type=text name=input id="in2" value=2 onclick=inputs(this)>
<input type=text name=input id="in3" value=3 onclick=inputs(this)>
<input type=text name=input id="in4" value=4 onclick=inputs(this)>
<script type=text/javascript>
function inputs(dis)
{
var val=dis.value;
document.write(val);
}
</script>

Can you try this, Added this in inputs(this) 你能尝试一下吗,在inputs(this)添加this inputs(this)

HTML: HTML:

<input type=text name=input id="in1" value=1 onclick=inputs(this)>

Javascript: 使用Javascript:

<script type=text/javascript>
function inputs(d)
{   
   document.write(d.value);
}
</script>

Try this 尝试这个

<input type=text name=input id="in1" value=1 onclick=inputs(this)>
<input type=text name=input id="in2" value=2 onclick=inputs(this)>
<input type=text name=input id="in3" value=3 onclick=inputs(this)>
<input type=text name=input id="in4" value=4 onclick=inputs(this)>
<script type=text/javascript>
function inputs(el)
{
 var val=el.getAttribute('id');
 document.write(val);
}
</script>
<input type="text" name="input" id="in1" value="1" onclick="inputs(this)">
<input type="text" name="input" id="in2" value="2" onclick="inputs(this)">
<input type="text" name="input" id="in3" value="3" onclick="inputs(this)">
<input type="text" name="input" id="in4" value="4" onclick="inputs(this)">
<script type=text/javascript>
function inputs(elem)
{
var val=elem.id;
console.log(val);
}
</script>

Check value in console 检查控制台中的值

Try 尝试

onclick=inputs(this)

And

function inputs(element)
{
   var val = element.id;
   // rest of the code here
}

It will get id of clicked input field.Try this: 它将获得点击输入字段的id。试试这个:
DEMO : http://jsfiddle.net/6Z2fR/ 演示: http //jsfiddle.net/6Z2fR/

<input type=text name=input id="in1" value=1 onclick=inputs(this)>
<input type=text name=input id="in2" value=2 onclick=inputs(this)>
<input type=text name=input id="in3" value=3 onclick=inputs(this)>
<input type=text name=input id="in4" value=4 onclick=inputs(this)>

  <script type=text/javascript>
        function inputs(e)
        {
            var val=e.id;
            console.log(val);
        }
    </script>

Most easiest way 最简单的方法

<input type="text" name="input" id="in1" value="1" onfocus="inputs(this.value)">
<script type=text/javascript>
function inputs(x)
{
document.write(x);
}
</script>

I think it is better to use jQuery, you can change event like mouseover/keydown/hover and make code not too mess :P 我认为使用jQuery更好,你可以改变像mouseover / keydown / hover这样的事件并使代码不会太乱:P

<input type="text" name="input" id="in1">
<input type="text" name="input" id="in2">
<input type="text" name="input" id="in3">
<input type="text" name="input" id="in4">

<script type="text/javascript">
(function ($) {
    $('input[type="text"]').each(function () {
        $(this).click(function () {
            alert($(this).attr('id'));
        });
    });
})(jQuery);

Or just see the demo in JSFiddle, click me! 或者只是在JSFiddle中看到演示, 点击我!

Try This: 尝试这个:

<input type="text" name="input" id="in1" value="1" onclick="inputs(this)">
<input type="text" name="input" id="in2" value="2" onclick="inputs(this)">
<input type="text" name="input" id="in3" value="3" onclick="inputs(this)">
<input type="text" name="input" id="in4" value="4" onclick="inputs(this)">

function inputs(element) {
    var id = element.id;
    var val = element.name;
    alert(val ,id);
}

fiddle Demo 小提琴演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得4的“ id” <div> 和4 <input type=“text”> - how can I get the 'id' of 4 <div> and 4 <input type=“text”> 如何通过 ID 将我的输入值传递给 javascript - How can i pass the value of my input to javascript through ID 通过使用名称而不是javascript中的ID获取输入类型文本的值 - Get value of input type text by using Name instead of Id in javascript 如何使用id输入type =“ text” javascript中的值设置自动2十进制数? - How to set auto 2 decimal number using value from id input type=“text” javascript? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 我将输入类型的 id 作为唯一 id,那么我怎样才能将这些唯一 id 放入 javascript 中? - i am having id of input type as unique id so how can i get those unique id into the javascript? 如何在输入类型文本中以id的形式显示结果数据? - How to show result data in id follow value in input type text? 如何获取输入类型的动态ID? - How can I get the id the dynamic ID of input type? 如何将一个JSP的输入类型值或ID传递给另一个JSP页面 - How can I pass input type value or id from one jsp to another jsp page 如何通过onchange函数将带有选项值的输入类型的PHP值传递给Javascript? - How can I pass PHP value of an input type from onchange function with the option value to Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM