Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么此代码在使用表单标记时会导致错误?_Javascript_Html - Fatal编程技术网

Javascript 为什么此代码在使用表单标记时会导致错误?

Javascript 为什么此代码在使用表单标记时会导致错误?,javascript,html,Javascript,Html,我编写了JavaScript代码,通过onclick事件在textbox中显示特定的div标记 如果我使用表单标记,它不会通过onclick在textfield中显示div标记id。如果我删除表单标签,它可以正常工作。为什么? 资料来源: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> &l

我编写了JavaScript代码,通过onclick事件在textbox中显示特定的div标记

如果我使用表单标记,它不会通过onclick在textfield中显示div标记id。如果我删除表单标签,它可以正常工作。为什么?

资料来源:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function MMID(clicked_id) {
    document.getElementById('MMID').value = clicked_id;
}
</script>
</head>
<body>
<form action="" method="post">
    <div id="28" onclick="MMID(this.id)">Hello Ram</div>
    <input name="MMID" type="text" id="MMID" />
</form>
</body>
</html>

无标题文件
功能MMID(单击的\u id){
document.getElementById('MMID')。value=clicked\u id;
}
你好,拉姆

应该更改函数名和id名。检查控制台,它会给您一个错误

 <script type="text/javascript">
 function MMID_FUNC(clicked_id)
 {
   document.getElementById('MMID').value=clicked_id;

 }
</script>

<div id="28" onclick="MMID_FUNC(this.id)">Hello Ram</div>
   <input name="MMID" type="text" id="MMID" />

函数MMID\u FUNC(单击\u id)
{
document.getElementById('MMID')。value=clicked\u id;
}
你好,拉姆
试试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function MMID(clicked_id)
{
document.getElementById('MMID').value=clicked_id;

}
</script>
</head>
<body>
<form action="" method="post">
<div id="28" onclick="javasxcript: MMID(this.innerHTML)">Hello Ram</div>
<input name="MMID" type="text" id="MMID" />
</form>
</body>
</html>

无标题文件
功能MMID(单击的\u id)
{
document.getElementById('MMID')。value=clicked\u id;
}
你好,拉姆
使用以下代码:

<div id="28" class="mycls">Hello Ram</div>

$('.mycls').click(function(){
    clicked_id = $(this).attr('id');
    $('#MMID').val(clicked_id);
}); 
你好,Ram $('.mycls')。单击(函数(){ 单击_id=$(this.attr('id'); $('#MMID').val(单击#id); }); 您需要在页面上加载jquery以使用上述代码,但这非常简单

如果有任何疑问,请告诉我

谢谢

试试这个:

<div id="28" onClick="MMID(this);">Hello Ram</div>
<input name="MMID" type="text" id="MMID" />

无标题文件
功能mmid(单击的\u id){
document.getElementById('MMID')。value=clicked\u id;
}
你好,拉姆

不要在表单标记中为函数和div id指定相同的名称。 Javascript认为MMID是一个对象,而不是一个函数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script type="text/javascript">
            function gets(clicked_id)
            {
                document.getElementById('MMID').value=clicked_id;

            }
        </script>
    </head>
    <body>
        <form action="tests.php" method="post">
            <div id="28" onclick="gets(this.id)">Hello Ram</div>
            <input name="MMID" type="text" id="MMID" />
        </form>
    </body>
</html> 

无标题文件
函数获取(单击\u id)
{
document.getElementById('MMID')。value=clicked\u id;
}
你好,拉姆

不显示任何错误。如果使用表单标记,单击功能不工作。如果删除表单标记,则单击功能不工作fine@user3128717没问题。如果问题已解决,请将答案标记为正确。谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
        function mmid(clicked_id) {
            document.getElementById('MMID').value = clicked_id;
        }
    </script>
</head>
<body>
<form action="" method="post">
    <div id="28" onclick="mmid(this.id)">Hello Ram</div>
    <input name="MMID" type="text" id="MMID" value="" />
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script type="text/javascript">
            function gets(clicked_id)
            {
                document.getElementById('MMID').value=clicked_id;

            }
        </script>
    </head>
    <body>
        <form action="tests.php" method="post">
            <div id="28" onclick="gets(this.id)">Hello Ram</div>
            <input name="MMID" type="text" id="MMID" />
        </form>
    </body>
</html>