这就是场景。我有 2 个下拉菜单。第一个具有 onchange 函数来加载第二个下拉列表中的数据。它完全可以工作,但我想使用 onchange 函数 onload 在第二个下拉列表中加载数据。
这是我的函数:
function fetch_select(val)
{
$.ajax({
type: 'post',
url: '../function/fetch_car_package.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response;
}
});
}
这是我的下拉列表:
<select name='cItemID' onchange="fetch_select(this.value);">
<?php
$sql = "";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
foreach($result as $row) {
if ($_SESSION['cItemID'] == $row['Item ID']) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='".$row['Item ID']."' $selected>".$row['Item Name']."</option>";
}
?>
</select>
我的ajax处理页面:
if(isset($_POST['get_option'])){
$ItemID = $_POST['get_option'];
$sql = "";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
foreach($result as $row) {
echo "<option value='".$row['cCLID']."' $selected>".$row['cDescription']."</option>";
}
exit;
}
最佳答案
如果可以的话,我(个人)不喜欢运行内联 javascript,所以我会使用 $(document).ready() 作为load 和 .on('change') 用于更改操作,两者都使用相同的函数:
<script>
// I am making an object for ajax reuse. You don't necessarily have to have this...premise is the same.
var Ajaxer = function($)
{
var $j = $;
var useUrl = '../function/fetch_car_package.php';
var url;
this.setUrl = function(url)
{
useUrl = url;
return this;
}
this.ajax = function(data,func)
{
$j.ajax({
type: 'post',
url: useUrl,
data: data,
success: function (response) {
func(response);
}
});
}
}
// When document loads
$(document).ready(function() {
// Create ajax instance, pass jQuery
var nAjax = new Ajaxer($);
// Set the send function
function getSelect(obj)
{
// Save the value
var val = obj.val();
// Run the ajax
nAjax.ajax({
"get_option":val
},
function(response){
// Send data to our container
$("#new_select").html(response);
}
);
}
// On load, save our select object
var SelectMenu = $('select[name=cItemID]');
// Run the function on load
getSelect(SelectMenu);
// On change, run the function
SelectMenu.on('change',function(e) {
// Use $(this) to reference itself
getSelect($(this));
});
});
</script>
关于javascript - AJAX 在页面加载之前运行 onchange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38449316/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送