我怎么能把它放在一个函数中

时间:2015-12-01 23:39:51

标签: javascript jquery

大家好,我可以把它放在一个函数中,然后调用它:

  $(".submit").click(function(){
    var val_text_ = $('.submit_form input[name="Title"]').val();
    var val_body_ = $('.submit_form textarea').val();   
    var val_link_ = $('.submit_form input[name="Hyperlink"]').val();
    var val_link_description_ = $('.submit_form input[name="HyperlinkDescription"]').val();
    var val_date_time_ = $('.submit_form input[name="DateTime"]').val();
    var val_deliverables_ = $('.submit_form select[name="DropListValueDeliverables"]').val();   
    var val_groupvalue_ = $('.submit_form input[name="DropListMeasureGroupValue"]').val();
    var result_deliverables_ = result_of_option_.text();
    createListItem(siteUrl, val_text_, val_body_, val_date_time_, val_link_, val_link_description_, result_deliverables_);
 });

谢谢!

2 个答案:

答案 0 :(得分:1)

查看javascript函数文档man lsof

但我相信这是函数中的更新代码......

   function yourFunction() {
  $(".submit").click(function(){
    var val_text_ = $('.submit_form input[name="Title"]').val();
    var val_body_ = $('.submit_form textarea').val();   
    var val_link_ = $('.submit_form input[name="Hyperlink"]').val();
    var val_link_description_ = $('.submit_form input[name="HyperlinkDescription"]').val();
    var val_date_time_ = $('.submit_form input[name="DateTime"]').val();
    var val_deliverables_ = $('.submit_form select[name="DropListValueDeliverables"]').val();   
    var val_groupvalue_ = $('.submit_form input[name="DropListMeasureGroupValue"]').val();
    var result_deliverables_ = result_of_option_.text();
    createListItem(siteUrl, val_text_, val_body_, val_date_time_, val_link_, val_link_description_, result_deliverables_);
 });
}

答案 1 :(得分:1)

如果你给你的函数命名,只要你想调用它就可以独立调用它。

function foo(){
    var val_text_ = $('.submit_form input[name="Title"]').val();
    var val_body_ = $('.submit_form textarea').val();   
    var val_link_ = $('.submit_form input[name="Hyperlink"]').val();
    var val_link_description_ = $('.submit_form input[name="HyperlinkDescription"]').val();
    var val_date_time_ = $('.submit_form input[name="DateTime"]').val();
    var val_deliverables_ = $('.submit_form select[name="DropListValueDeliverables"]').val();   
    var val_groupvalue_ = $('.submit_form input[name="DropListMeasureGroupValue"]').val();
    var result_deliverables_ = result_of_option_.text();
    createListItem(siteUrl, val_text_, val_body_, val_date_time_, val_link_, val_link_description_, result_deliverables_);
 }

一旦写完,这个函数将响应foo的名称,只需添加大括号并调用它

foo();

如果您需要,您也可以在其他活动的功能主体中调用它

$('#otherElement').keypress(function(){
   //Do your stuff

   foo();
};