简体   繁体   English

从HTML表单中删除元素?

[英]Removing element from HTML form?

I have element in my HTML form which i want to remove during on load Here is the element in the form 我的HTML表单中有一个元素,要在加载过程中删除,这是表单中的元素

 <input type="hidden" name="testToken" value="1000ad"></input>

what I tried 我尝试过的

  $("input[name='testToken']").remove();

But i still see the element in HTML when i do view page source . 但是当我查看页面源代码时,我仍然看到HTML中的元素。 I debugged it in Firefox , this line gets executed successfully without any error in console. 我在Firefox中对其进行了调试,该行已成功执行,并且在控制台中没有任何错误。

what is the issue here ? 这是什么问题?

UPDATE:- 更新: -

But if i do below it works. 但是,如果我做不到它的作品。 I do not see element when i do view page source. 查看页面源代码时看不到元素。 But i 但是我

 var testElement = document.getElementsByName('testToken');
 testElement[0].remove();

But testElement[0].remove(); 但是testElement[0].remove(); throws error in IE saying Object doesn't support property or method 'remove' 在IE中引发错误,称Object doesn't support property or method 'remove'

The changes from js is not solid but it is temporary. js的更改不是很牢固,但只是暂时的。

$("input[name='testToken']").remove();

This line of code removes the DOM element but this can be seen if you see the source of the page via Ctrl + U for view src. 这行代码删除了DOM元素,但是如果您通过Ctrl + U来查看页面src的页面源,则可以看到该元素。

Instead you should do it serverside. 相反,您应该在服务器端执行此操作。

With PHP as a server side you can do something like: 使用PHP作为服务器端,您可以执行以下操作:

<?php if($somevar != "" || $somevar != null) { ?>
   <input type="hidden" name="testToken" value="1000ad"></input>
<?php } ?>

just try removing the element with the name attribute, may this will help 只需尝试删除具有name属性的元素,这可能会有所帮助

 function removeInput(){ $("[name=testToken]").remove(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <input type="text" name="testToken" value="blabla" /><br/> <input type="button" onclick="removeInput()" value="remove" /> </body> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM