我有一个输入字段和一个按钮。现在我希望它只接受包含我选择的单词的文本。
<input type="hidden" name="post_id" id="fep-post-id" value="<?php echo $post_id ?>">
<button type="button" id="fep-submit-post" class="active-btn">Go</button><img class="fep-loading-img" src="<?php echo plugins_url( 'static/img/ajax-loading.gif', dirname(__FILE__) ); ?>"/>
这是一个提交表单,人们只能提交来自特定网站的网址。它是 WordPress 插件中的 php 文件
我尝试了脚本解决方案,但我认为它与插件scripts.js 混淆了。我假设我需要在代码片段中添加一些代码:
$("#fep-submit-post.active-btn").on('click', function() {
tinyMCE.triggerSave();
var title = $("#fep-post-title").val();
var content = $("#fep-post-content").val();
var bio = $("#fep-about").val();
var category = $("#fep-category").val();
var tags = $("#fep-tags").val();
var pid = $("#fep-post-id").val();
var fimg = $("#fep-featured-image-id").val();
var nonce = $("#fepnonce").val();
var message_box = $('#fep-message');
var form_container = $('#fep-new-post');
var submit_btn = $('#fep-submit-post');
var load_img = $("img.fep-loading-img");
var submission_form = $('#fep-submission-form');
var post_id_input = $("#fep-post-id");
var errors = post_has_errors(title, content, bio, category, tags, fimg);
if( errors ){
if( form_container.offset().top < $(window).scrollTop() ){
$('html, body').animate({ scrollTop: form_container.offset().top-10 }, 'slow'); }
message_box.removeClass('success').addClass('warning').html('').show().append(errors);
return;
}
<小时/>
这是我使用的 2 个文件:
脚本.js
表单.php
===>>>>>> 和 <<<<====== 之间是我需要验证的输入字段。 下面是提交按钮。 http://pastebin.com/G53HScu3
最佳答案
我会这样做
<form onsubmit="return validator()">
<input type="hidden" name="post_id" id="fep-post-id" value="<?php echo $post_id ?>">
<button type="button" id="fep-submit-post" class="active-btn">Go</button><img class="fep-loading-img" src="<?php echo plugins_url( 'static/img/ajax-loading.gif', dirname(__FILE__) ); ?>"/>
</form>
<script type="text/javascript">
var validator = function(){
switch (variable) {
case "allowed":
submit form;
break;
default:
return false;
}
}
</script>
在 onsubmit 属性中返回 false 将阻止表单提交。我只是给出了一个基本示例,使用“允许”,但您可能必须使用字符串函数来确定基本 URL 是否是您信任的网站之一。
可能有更好的方法来做到这一点 - 也许使用选择 - 但只需稍加修改,这应该可以实现您的目标。
关于javascript - html <输入需要包含特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346268/