如何在包含弹出窗口的JavaScript中访问表单元素

时间:2017-11-09 07:36:21

标签: javascript html css

如何在javascript中使用id="namesignup"访问输入?

我尝试使用

console.log("Result :"+ $$('.popup-signup-email #namesignup'));
document.getElementById(namesignup)

两者都不工作..有没有如何访问输入中的数据? 请帮忙

<div data-page="signup" class="page no-navbar">
<!-- Begin: Page Content -->
<div class="page-content">
<div class="content-block"> </div>
<div class="content-block">
<p>
<button class="button big"><span>Connect with Facebook</span></button>
</p>
<p>
<button class="button"><span>Connect with Google</span></button>
</p>
<p>
<a href="#" class="button open-popup" data-popup=".popup-signup-email">
<span>Sign Up with Email</span>
</a>
</p>
</div>
</div>
<!-- End: Page Content -->

<!-- Begin: Popup -->
<div class="popup tablet-fullscreen popup-signup-email">
<div class="toolbar">
<div class="toolbar-inner">
<div class="left">
<a href="#" class="link disabled"></a>
</div>
<div class="center">Sign Up</div>
<div class="right">
<a href="#" class="link close-popup">
</a>
</div></div></div>

<div class="container">
<div class="content-block"> </div>
<form name="signup-email" action="#" method="POST" enctype="multipart/form-data">
<div class="list-block no-hairlines">
<ul>
<li>
<div class="item-content">
<div class="item-media"></div>
<div class="item-inner">
<div class="item-input">
<input id="namesignup" type="text" name="name" placeholder="Name" required />
</div>

4 个答案:

答案 0 :(得分:0)

  1. 确保您配置了jQuery&amp;检查别名。默认情况下,$是jQuery的别名。如果配置正确,下面的代码将起作用,

    $(&#39;#namesignup&#39)。VAL()

  2. 2)如果你使用原生JavaScript,

    document.getElementById('namesignup').value
    

答案 1 :(得分:0)

尝试以下代码。此代码在namesignup中查找更改并将其打印在控制台中,

&#13;
&#13;
<div data-page="signup" class="page no-navbar">
<!-- Begin: Page Content -->
<div class="page-content">
<div class="content-block"> </div>
<div class="content-block">
<p>
<button class="button big"><span>Connect with Facebook</span></button>
</p>
<p>
<button class="button"><span>Connect with Google</span></button>
</p>
<p>
<a href="#" class="button open-popup" data-popup=".popup-signup-email">
<span>Sign Up with Email</span>
</a>
</p>
</div>
</div>
<!-- End: Page Content -->

<!-- Begin: Popup -->
<div class="popup tablet-fullscreen popup-signup-email">
<div class="toolbar">
<div class="toolbar-inner">
<div class="left">
<a href="#" class="link disabled"></a>
</div>
<div class="center">Sign Up</div>
<div class="right">
<a href="#" class="link close-popup">
</a>
</div></div></div>

<div class="container">
<div class="content-block"> </div>
<form name="signup-email" action="#" method="POST" enctype="multipart/form-data">
<div class="list-block no-hairlines">
<ul>
<li>
<div class="item-content">
<div class="item-media"></div>
<div class="item-inner">
<div class="item-input">
<input id="namesignup" type="text" name="name" placeholder="Name" required />
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$("#namesignup").keyup(function(){
	console.log($("#namesignup").val());
});
</script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

正如其他人在后面的帖子中提到的那样:

必须使用document.getElementById('namesignup').value代码,但如果您有多个带有&#39; namesignup&#39; Id然后此代码返回具有此id的第一个元素值。

如果您使用的是Google Chrome,请按F12 - &gt;转到元素标签 - &gt;搜索&#39; namesignup&#39;。

请确保您有一个具有此ID的元素。

答案 3 :(得分:0)

  1. 在javascript中使用document.getElementById(&#34; namesignup&#34;)。value
  2. 在jQuery中使用$(&#34; #nameignignup&#34;)。value()