admin管理员组文章数量:1355602
I have a form which looks like this
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
... other stuff
</form>
In a mon $(document).ready(function()
which is shared between multiple pages, I want to detect if I am in a page which has a form with the following characteristics
- action="/aa//bbcc" & id="Form1"
- an input with name="somename" and value="somevalue"
How do I do this?
The page has access to jquery also if that is required. Without jquery is also fine.
I have a form which looks like this
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
... other stuff
</form>
In a mon $(document).ready(function()
which is shared between multiple pages, I want to detect if I am in a page which has a form with the following characteristics
- action="/aa//bbcc" & id="Form1"
- an input with name="somename" and value="somevalue"
How do I do this?
The page has access to jquery also if that is required. Without jquery is also fine.
Share Improve this question asked Jul 13, 2017 at 4:31 user93353user93353 14.1k8 gold badges62 silver badges126 bronze badges 3-
No-one tells that how he will check the current page and match it ? Because it's a mon
$(document).ready(function(){..});
so OP have to check that-one first – Death-is-the-real-truth Commented Jul 13, 2017 at 4:54 - @AlivetoDie what do you mean? – user93353 Commented Jul 13, 2017 at 4:57
- user93353 if you are asking me what it means , then you can go for any-one answer. All are correct. Just forget about my ment. – Death-is-the-real-truth Commented Jul 13, 2017 at 4:59
4 Answers
Reset to default 4you can select the form with id and check if the node list returned is of length more than 0. Then you can check if it has an input with name 'somename' and then check if the value of that input is 'somevalue'.
if ($('#Form1[action="/aa/bbcc"]').length > 0 && $('#Form1').has("input[name='somename']") &&
$("#Form1 input[name='somename']").val() === "somevalue"){
console.log('found');
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
... other stuff
</form>
You can try the following
$(document).ready(function() {
function isFormPresent() {
return !!$("form#Form1[action='/aa/bbcc'] input[name='somename'][value='somevalue']").length
}
console.log(isFormPresent());
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
</form>
Try with in selector
For single line
$(document).ready(function() {
if ($('form[action="/aa/bbcc"]').find('input[name="somename"][value="somevalue"]').length > 0) {
console.log('ok')
} else {
console.log('not ok')
}
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
</form>
Separate
$(document).ready(function() {
if ($('form[action="/aa/bbcc"]').length > 0) {
console.log('action ok')
}
if ($('form[action="/aa/bbcc"]').find('input[name="somename"]').length > 0) {
console.log('input name ok')
}
if ($('form[action="/aa/bbcc"]').find('input[value="somevalue"]').length > 0) {
console.log('input value ok')
}
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" />
</form>
Check the following example .
if (document.querySelector("#Form1") && (document.getElementById("Form1").getAttribute("action") == '/aa/bbcc')) {
var elem = document.getElementById("Form1").children[0];
if (elem.getAttribute('name') == 'somename' && elem.getAttribute('value') == 'somevalue') {
// code to exceute
console.log('in result');
}
}
<form action="/aa/bbcc" method="post" id="Form1" accept-charset="UTF-8">
<input type="hidden" name="somename" value="somevalue" /> ... other stuff
</form>
版权声明:本文标题:jquery - Javascript: How to detect if the current page has a form with particular name and input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743994259a2572702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论