我正在使用Python和CGI开发Web应用程序。对于表单验证,我打算使用JavaScript。这是场景 - 有一个文本框,用户在其中输入项目名称并单击提交按钮。 如果文本框中没有输入任何内容并发出警报,我想抓住。
对于JavaScript函数,我使用了参数,project_form(用于表单)和project_name用于文本框。但我不清楚的是在哪里放这个功能。截至目前,我正试图将其放在表单标签中。但它没有用。我不知何故认为我必须将project_form和project_name参数传递给函数,并且由于代码结构,我不可能将函数放在'head'部分。我在这里粘贴我的代码。谢谢你的帮助。
<form action="create_project.py" method="POST" name="project_form">
<input type="text" name="project_name" size="40" onclick="javascript:document.project_form.submit.disabled=false"
id="acpro_inp0">
<br>
<script type="text/javascript">
function ValidateTextbox(project_form, project_name) {
var tb_value = project_form.project_name.value;
if (tb_value == null || tb_value.trim() == "") {
alert("Please enter something");
project_form.project_name.focus();
return false;
}
return true;
}
</script>
return ValidateTextbox('project_form','project_name')
<p>
</p>
<input type="submit" name="submit" value="Submit" onsubmit="return ValidateTextbox('project_form','project_name')"
disabled="">
</form>
以下是相关的Python代码 -
import yate
import jshelper
print yate.start_response()
print yate.include_header("Create a new project.")
#Section to create a new project
print yate.start_form("create_project.py","project_form")
print yate.text_box("project_name",form_name="project_form")
jshelper.JSTextBoxValidate("project_form","project_name")
print yate.end_form("Submit",is_disabled = 'True', onsubmit_callback = "return ValidateTextbox('project_form','project_name')")
print yate.include_footer(({"Home": "../index.html"}))
这是另一段Python代码,我尝试动态创建JS函数。
def JSTextBoxValidate(form_name,tb_name):
print '<script type="text/javascript">'
print 'function ValidateTextbox' + '(' + form_name + ',' + tb_name + ')'
print '{'
print 'var tb_value = ' + form_name + '.' + tb_name + '.' + 'value;'
print 'if (tb_value==null || tb_value.trim()=="")'
print '{'
print 'alert("Please enter something");'
print form_name + '.' + tb_name + '.' + 'focus();'
print 'return false;'
print '}'
print 'return true;'
print '}'
print '</script>'
不知怎的,我感觉我可能在这个过程中没有走上正轨,必须有更好的方法。所以我会感谢帮助我让这件事情发挥作用。 注 - 包yate来自Head First Python,我正在使用并扩展该代码中的模板。我在我的项目文档中添加了类似的注释。这是我的个人项目。
答案 0 :(得分:0)
以其应有的方式重写HTML代码:
<script type="text/javascript">
function ValidateTextbox(input) { //we take an object representation of the HTML <input> tag (not just a string)
if (input.value.trim() == "") { //the attribute value is already defined in the tag now, so it couldn't be null
alert("Please enter something");
input.focus(); //move the cursor to the incriminated input
return false; //preventing from submitting the form (assuming the function is called as a return value)
}
return true; //allowing the form to be submitted (assuming the function is called as a return value)
}
//function added a you seemed to need something like that
function DisableFormSubmit(theForm, e) { //we take the form object as argument, and e may be the event object given from the keypress event
var submitButton=theForm.submit; //to ease typing this code, the submit button is assigned to a local variable
if(theForm.project_name.value.trim()=='') { //if we have no value for the project_name input of the form
submitButton.disabled=true; //we disable the submit button
submitButton.value="Submit - enter a name first"; //and we give a hint why it is disabled in the text printed on the button
} else { //in this case project_name has a valid value
submitButton.disabled=false; //the submit button is enabled
submitButton.value="Submit"; //we remove the hint we gave
}
//processing the event now, to know what to return (key 13 is Enter, returning false on it will prevent the form from being submitted by pressing Enter)
if(window.event) //MSIE compatibility
return window.event.keyCode!=13;
else if(e) //web browser's behavior
return e.which!=13;
}
}
</script>
<form action="create_project.py" method="POST" name="project_form" onsubmit="return ValidateTextBox(this.project_name);">
<input type="text" name="project_name" value="" size="40" onkeypress="return DisableFormSubmit(this.form, event);" id="acpro_inp0">
<br>
<input type="submit" name="submit" value="">
</form>
<script type='text/javascript'>
DisableFormSubmit(document.forms['project_form']); //we call this function once after the <form> has loaded so that we init the submit button with a standard way
</script>
对于Python代码,我无法分辨,因为我不知道这种语言,但您不需要“动态”创建JS函数来对表单的有效性进行客户端检查。注意客户端的事情,意思是不安全,这意味着如果您在该字段中绝对需要一个值,那么请在服务器端进行第二次检查(显然使用Python)。你的问题是:
<script></script>
标记'project_form'
和'project_name'
- 注意引号 - 你需要像document.project_form
或{{1并且附加了document.forms['project_form']
之前的其中一个。请注意这里没有引号。.project_name
中使用了属性onsubmit
,而其位置应位于<input>
中(尽管此点可能不是错误的原因)希望这会有所帮助;)